/*
 * Created by JFormDesigner on Tue Jun 13 11:42:08 CST 2023
 */

package fm.trade;

import java.awt.event.*;

import dao.Dao_trade;
import model.Trade;

import java.awt.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class Select_trade extends JInternalFrame {

    private String [] columnNames = {"商品编号", "商品名称", "商品类别", "数量", "折扣", "单价"};

    private Object [][] getFS(List list)
    {
        Object [][] results = new Object[list.size()][columnNames.length];

        for(int i = 0; i < list.size(); i++)
        {
            Trade trade = (Trade) list.get(i);

            results[i][0] = trade.getTradeID();
            results[i][1] = trade.getTradeName();
            results[i][2] = Dao_trade.getTradeName_ID( trade.getTypeID());
            results[i][3] = trade.getNumber();
            results[i][4] = trade.getDiscount();
            results[i][5] = trade.getPrice();
        }
        return results;
    }
    public Select_trade() {
        initComponents();
    }

    private void Search(ActionEvent e) {
        // TODO add your code here
        String search = (String) comboBox1.getSelectedItem();

        if(search.equals("商品编号"))
        {
            Object [][] results = getFS(Dao_trade.fuzzySelect_1(textField1.getText().trim()));
            DefaultTableModel model = new DefaultTableModel(results, columnNames);
            table1.setModel(model);
        }

        if(search.equals("商品名称"))
        {
            Object [][] results = getFS(Dao_trade.fuzzySelect_2(textField1.getText().trim()));
            DefaultTableModel model = new DefaultTableModel(results, columnNames);
            table1.setModel(model);
        }
    }

    private void Cancel(ActionEvent e) {
        // TODO add your code here
        doDefaultCloseAction();
    }

    private void textField1KeyTyped(KeyEvent e) {
        // TODO add your code here
        if(e.getKeyChar() == '\n')
        {
            SearchButton.doClick();
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        comboBox1 = new JComboBox<>();
        textField1 = new JTextField();
        panel3 = new JPanel();
        SearchButton = new JButton();
        CancelButton = new JButton();
        panel4 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setVisible(true);
        setIconifiable(true);
        setClosable(true);
        setTitle("\u67e5\u8be2\u5546\u54c1");
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(0, 60));
            panel1.setLayout(null);

            //---- comboBox1 ----
            comboBox1.setModel(new DefaultComboBoxModel<>(new String[] {
                "\u5546\u54c1\u540d\u79f0",
                "\u5546\u54c1\u7f16\u53f7"
            }));
            panel1.add(comboBox1);
            comboBox1.setBounds(191, 15, 95, comboBox1.getPreferredSize().height);

            //---- textField1 ----
            textField1.addKeyListener(new KeyAdapter() {
                @Override
                public void keyTyped(KeyEvent e) {
                    textField1KeyTyped(e);
                }
            });
            panel1.add(textField1);
            textField1.setBounds(296, 15, 175, textField1.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1, BorderLayout.NORTH);

        //======== panel3 ========
        {
            panel3.setPreferredSize(new Dimension(397, 35));
            panel3.setLayout(null);

            //---- SearchButton ----
            SearchButton.setText("\u67e5\u8be2");
            SearchButton.addActionListener(e -> Search(e));
            panel3.add(SearchButton);
            SearchButton.setBounds(new Rectangle(new Point(215, 4), SearchButton.getPreferredSize()));

            //---- CancelButton ----
            CancelButton.setText("\u53d6\u6d88");
            CancelButton.addActionListener(e -> Cancel(e));
            panel3.add(CancelButton);
            CancelButton.setBounds(new Rectangle(new Point(350, 4), CancelButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel3, BorderLayout.SOUTH);

        //======== panel4 ========
        {
            panel4.setBorder(new TitledBorder("\u5546\u54c1\u4fe1\u606f"));
            panel4.setLayout(null);

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(table1);
            }
            panel4.add(scrollPane1);
            scrollPane1.setBounds(10, 20, 635, 260);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel4.getComponentCount(); i++) {
                    Rectangle bounds = panel4.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel4.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel4.setMinimumSize(preferredSize);
                panel4.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel4, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JComboBox<String> comboBox1;
    private JTextField textField1;
    private JPanel panel3;
    private JButton SearchButton;
    private JButton CancelButton;
    private JPanel panel4;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
